package com.yupi.springbootinit.bizmq;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static com.yupi.springbootinit.constant.MqConstant.EXCHANGE;
import static com.yupi.springbootinit.constant.MqConstant.ROUTING_KEY;

@Component
public class BiMessageProducer {
    @Resource
    private RabbitTemplate rabbitTemplate;


	/**
     * 发送消息的方法
     *
     * @param chartId  当前文件生成的图表id
     * @param csvData    消息内容，要发送的具体消息 csvData excel文件解析后的文本
     */
    public void sendMessage(String csvData,String chartId,String userId) {
        // 使用rabbitTemplate的convertAndSend方法将消息发送到指定的交换机和路由键
        rabbitTemplate.convertAndSend(EXCHANGE, ROUTING_KEY, csvData, message -> {
            message.getMessageProperties().setHeader("chartId", chartId);
            message.getMessageProperties().setHeader("userId", userId);
            return message;
        });
    }
}
